<?php
namespace app\admin\controller;

use app\admin\api\ConferenceIntroduction;
use app\admin\controller\Permissions;
use think\Cache;
use think\Session;


/**
 * 会议站--站点介绍管理
 */
class Conferintr extends  Permissions{

    /**
     * 获取站点介绍列表
     */
    public function list($sid){

        //换区网站token
        $token = getapptoken($sid);

        $data = new ConferenceIntroduction();

        $lan = Session::get("lang[$sid]");

        if($lan == 'en,zh-CN'){
            $lm = 0;
        }
        if($lan == 'zh-CN'){
            $lm = 1;
        }
        if($lan == 'en'){
            $lm = 2;
        }

        //接收数据
        if($this->request->isPost()){

            $post = $this->request->post();
            $res = $data->introductionList($token,$post);

            $site = $res->items;

            if(empty($site)){
                $this->assign('intrs',-1);
            }else{
                $intrs = getstandardconent($site);
                $intrs[0]['lm'] = $lm;
                $intrs[0]['sid'] = $sid;
                return json($intrs);
            }
        }else{
            $fy['page'] = 1;
            $fy['limit'] = 10;

            $res = $data->introductionList($token,$fy);
            $site = $res->items;

            if(empty($site)){
                $this->assign('intrs',-1);
                $this->assign('sid',$sid);
            }else{

                //列表总数
                $total = $res->meta->total;
                $intrs = getstandardconent($site);
                $intrs[0]['lm'] = $lm;
                $intrs[0]['sid'] = $sid;

                $this->assign('sid',$sid);
                $this->assign('intrs',$intrs);
                $this->assign('total',$total);
            }
            return view();
        }
    }

    /**
     * 创建站点介绍
     */
    public function create($sid){

        //获取站点语言
        $lang = getlang($sid);
        //获取当前时间错作为文件编号
        $time = date("Ymd-H-i-s");

        $this->assign('sid',$sid);
        $this->assign('lang',$lang);
        $this->assign('time',$time);

        return view();

    }

    public function add(){
        if($this->request->isPost()) {

            //接收站点数据
            $post = $this->request->post();
            $data['translations'] = $post['translations'];
            $sid = $post['sid'];

            //换区网站token
            $token = getapptoken($sid);

            $data2 = new ConferenceIntroduction();
            $result = $data2->introductionCreate( $data,$token);

            if ($result == 555){
                $this->success("提交成功");

            }else{
                $this->error("$result");
            }
        }
    }


    /**
     * 获取站点介绍详情
     * @return [type] [description]
     */
    public function details($sid,$id){

        //换取网站token
        $token = getapptoken($sid);

        $data = new ConferenceIntroduction();
        $res = $data->introductionDetails($id,$token);

        //获取站点语言
        $lang = getlang($sid);

        $this->assign('sid',$sid);
        $this->assign('lang',$lang);
        $this->assign('intr',$res);
        //var_dump($res);
        return view();
    }


    /**
     * 更新站点介绍
     */
    public function update(){
        if($this->request->isPost()) {

            $post = $this->request->post();
            $sid = $post['sid'];

            //换区网站token
            $token = getapptoken($sid);

            //接收数据
            $id = $post['uid'];
            $data['translations']= $post['translations'];

            $data2 = new ConferenceIntroduction();
            $res = $data2->introductionUpdate($id,$data,$token);

            if ($res == 555){
                $this->success("修改成功");

            }else{
                $this->error("$res");
            }
        }
    }

    /**
     * 删除站点介绍
     */
    public function delete(){
        if ($this->request->isPost()) {

            //接收数据
            $post = $this->request->post();
            $id = $post['id'];
            $sid = $post['sid'];

            //换区网站token
            $token = getapptoken($sid);

            //请求接口
            $data = new ConferenceIntroduction();

            if(!empty($post['lang'])){
                $lang = $post['lang'];
                $res = $data->introductionDeleteOne($id,$token,$lang);
            }else{
                $res = $data->introductionDelete($id,$token);
            }

            if ($res == 555) {
                $this->success("删除成功");

            } else {
                $this->error("$res");
            }
        }
    }

}